/* Class mainGUI
 * 
 * 
 * 
 */
package disasterGameProj;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.Timer;

public class mainGUI implements MouseListener, MouseMotionListener 
{
	
	Timer countdownTimer;
	
	public JFrame frame = null;
	public JLayeredPane lpane = null;
	
	public Map mainMap = null;
	public mainScreenGui mainScreenPanel = null;
	public TutorialGui tutorialPanel = null;
	public SelectionPageGui selectionPanel = null;
	public mapGui mapPanel = null;
	public JPanel bottomMsgPanel = null;
	public BottomMessagePanel bottomList = null;
	public VehicleGui vehicleScreen = null;
	
	public Sprite spriteObj = null;
	
	public int gameScreen = 0;
	public int purchaseScreen = 0;
	public int aboutToPurchase = 0;
	public int purchaseMade = 0;
	public int popDisasterTime = 600;
	
	mainGUI()
	{
		spriteObj = new Sprite();
	}
	
	public void createAndShow()
	{
		
		frame = new JFrame();
		
		lpane = new JLayeredPane();
		lpane.setLayout( null );
		
		mainScreenPanel = new mainScreenGui( spriteObj );
		tutorialPanel = new TutorialGui( spriteObj );
		selectionPanel = new SelectionPageGui( spriteObj );
		bottomMsgPanel = new JPanel();
		
		mainScreenPanel.setBounds(0, 0, 1200, 960 );
		mainScreenPanel.addMouseListener( this );
		
		tutorialPanel.setBounds(0, 0, 1200, 960 );
		tutorialPanel.addMouseListener( this );
		tutorialPanel.setVisible( false );
		
		selectionPanel.setBounds( 0, 0, 1200, 960 );
		selectionPanel.addMouseListener( this );
		selectionPanel.setVisible( false );
		
		bottomList = new BottomMessagePanel( spriteObj );
		bottomMsgPanel = bottomList.mainBottomPanel;
		bottomMsgPanel.setVisible( false );
		
		vehicleScreen = new VehicleGui();
		vehicleScreen.setBounds(0, 0, 1200, 960 );
		
		lpane.add( mainScreenPanel, new Integer( 0 ) );
		lpane.add( tutorialPanel, new Integer( 1 ) );
		lpane.add( selectionPanel, new Integer( 2 ) );
		lpane.add( bottomMsgPanel, new Integer( 4 ) );
		lpane.add( vehicleScreen, new Integer( 5 ) );
		
		frame.add( lpane );
		frame.setPreferredSize( new Dimension( 1200, 960 ) );
		frame.setResizable( false );
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible( true );
	}

	
	public void mouseClicked(MouseEvent event) 
	{
		int x = event.getX() / 48;
		int y = event.getY() / 48;

		System.out.println( "Mouse Clicked at location: [" + x + "]:[" + y + "]" );
		
		// 0: mainScreen, 1: tutorialScreen, 2: selectionScreen, 3: gameScreen, 4: endGameScreen, 5: disasterScreen
		switch( gameScreen )
		{
		case 0:
			if( x == 12 && y == 17 )
			{
				mainScreenPanel.setVisible( false );
				tutorialPanel.setVisible( true );
				gameScreen = 2;
			}
			else if( x == 6 && y == 17 )
			{
				mainScreenPanel.setVisible( false );
				selectionPanel.setVisible( true );
				gameScreen = 2;
			}
			else if( x == 18 && y == 17 )
				System.exit( 0 );		
			break;
		case 1:
			if( x == 18 && y == 17 )
				System.exit( 0 );
			break;
		case 2:
			if( x == 8 && y == 7 )
			{
				selectionPanel.setVisible( false );
				mainMap = new Map( spriteObj, "earthQuake" );
				mainMap.createMap();
				mapPanel = new mapGui( mainMap );
				mapPanel.setBounds( 0, 0, 1200, 960 );
				mapPanel.addMouseListener( this );
				mapPanel.addMouseMotionListener( this );
				lpane.add( mapPanel, new Integer( 3 ) );
				bottomMsgPanel.setVisible( true );
				countdownTimer = new Timer(1000, new CountdownTimerListener());
				bottomList.moneyLabel.setText( "$" + mainMap.money );
				countdownTimer.start();
				gameScreen = 3;
			}
			else if( x == 18 && y == 17 )
				System.exit( 0 );
			break;
		case 3:
			if( x == 23 && y == 18 )
				System.exit( 0 );
			manageGameClick( x, y );
			break;
		case 4:
			break;
		case 5:
			break;
		default:
			break;
		}	
	}

	// manages all of the in game click locations
	// purchaseScreen: allows for this function to know what purchasable screen is currently displayed
	// 0: no purchases available, 1: building options, 2: helipad options, 3: fireStation options, 4: hospital options, 5: road options 
	// purchaseMade: allows for this function to know what item was purchased
	// 0: Ambulance, 1: fire truck, 2: bus, 3: helicopter
	private void manageGameClick(int x, int y) 
	{
		if( x < 25 && y < 15 )
		{
			if( aboutToPurchase == 0 )
			{
				purgePurchaseWindow();
				
				if( mainMap.tileList[x][y].myName.equals( "B" ) )
				{
					if( mainMap.tileList[x][y].buildingObj.myName.equals( "B" ) )
					{
						if( mainMap.tileList[x][y].buildingObj.reEnforced == false )
						{
							purchaseScreen = 1;
							bottomList.regBuildingPanel.setVisible( true );
							mainMap.startTile = mainMap.tileList[x][y];
						}
						else
						{
							purchaseScreen = 0;
							bottomList.emptyPanel.setVisible( true );
							mainMap.startTile = null;
						}
					}
					else if( mainMap.tileList[x][y].buildingObj.myName.equals( "He" ) )
					{
						purchaseScreen = 2;
						bottomList.helipadPanel.setVisible( true );
						mainMap.startTile = mainMap.tileList[x][y];
					}
					else if( mainMap.tileList[x][y].buildingObj.myName.equals( "F" ) )
					{
						purchaseScreen = 3;
						bottomList.fireStationPanel.setVisible( true );
						mainMap.startTile = mainMap.tileList[x][y];
					}
					else 
					{
						purchaseScreen = 4;
						bottomList.hospitalPanel.setVisible( true );
						mainMap.startTile = mainMap.tileList[x][y];
					}
				}
				else if( mainMap.tileList[x][y].myName.equals( "R" ) )
				{
					if( mainMap.tileList[x][y].roadBlock == false && mainMap.tileList[x][y].tent == false )
					{
						purchaseScreen = 5;
						bottomList.roadPanel.setVisible(  true );
						mainMap.startTile = mainMap.tileList[x][y];
					}
					else
					{
						purchaseScreen = 0;
						bottomList.emptyPanel.setVisible( true );
						mainMap.startTile = null;
					}
				}
				else 
				{
					bottomList.emptyPanel.setVisible( true );
					mainMap.startTile = null;
				}
			}
			else
			{
				aboutToPurchase = 0;
				Tile[] tempPath = null;
				switch( purchaseMade )
				{
				case 0:// Ambulance
					if( mainMap.tileList[x][y].buildingObj != null && mainMap.tileList[x][y].buildingObj.myName.equals( "B" ) )
					{
						tempPath = mainMap.aStarAlgorithm( mainMap.startTile , mainMap.tileList[x][y] );
						
						vehicleScreen.mapImageArray[tempPath[ ( tempPath.length - 1) ].xLoc][tempPath[ (tempPath.length - 1) ].yLoc] = spriteObj.emtImg;
						vehicleScreen.repaint();
						
						mainMap.money -= 10000;
						bottomList.moneyLabel.setText( "$" + mainMap.money );
						bottomList.messageTextArea.append( String.valueOf( "Ambulance Sent to: [" + x + "]:[" + y + "]\n" ) );
						mainMap.tileList[x][y].buildingObj.ambulancePresent = true;
					}
					else
						bottomList.messageTextArea.append( "Please click a non-essential building!\n" );
				
					break;
				case 1:// fire truck
					if( mainMap.tileList[x][y].buildingObj != null && mainMap.tileList[x][y].buildingObj.myName.equals( "B" ) )
					{
						tempPath = mainMap.aStarAlgorithm( mainMap.startTile , mainMap.tileList[x][y] );
						
						vehicleScreen.mapImageArray[tempPath[ ( tempPath.length - 1) ].xLoc][tempPath[ (tempPath.length - 1) ].yLoc] = spriteObj.fireTruckImg;
						vehicleScreen.repaint();
						
						mainMap.money -= 10000;
						bottomList.moneyLabel.setText( "$" + mainMap.money );
						bottomList.messageTextArea.append( String.valueOf( "fire truck Sent to: [" + x + "]:[" + y + "]\n" ) );
						mainMap.tileList[x][y].buildingObj.fireTruckPresent = true;
					}
					else
						bottomList.messageTextArea.append( "Please click a non-essential building!\n" );
					break;
				case 2:// bus
					if( mainMap.tileList[x][y].buildingObj != null && mainMap.tileList[x][y].buildingObj.myName.equals( "B" ) )
					{
						tempPath = mainMap.aStarAlgorithm( mainMap.startTile , mainMap.tileList[x][y] );
						
						vehicleScreen.mapImageArray[tempPath[ ( tempPath.length - 1) ].xLoc][tempPath[ (tempPath.length - 1) ].yLoc] = spriteObj.busImg;
						vehicleScreen.repaint();
						
						mainMap.money -= 10000;
						bottomList.moneyLabel.setText( "$" + mainMap.money );
						bottomList.messageTextArea.append( String.valueOf( "Bus Sent to: [" + x + "]:[" + y + "]\n" ) );
						mainMap.tileList[x][y].buildingObj.busPresent = true;
					}
					else
						bottomList.messageTextArea.append( "Please click a non-essential building!\n" );
					break;
				case 3:// helicopter
					if( mainMap.tileList[x][y].buildingObj != null && mainMap.tileList[x][y].buildingObj.myName.equals( "B" ) )
					{
						tempPath = mainMap.aStarAlgorithm( mainMap.startTile , mainMap.tileList[x][y] );
						
						vehicleScreen.mapImageArray[tempPath[ ( tempPath.length - 1) ].xLoc][tempPath[ (tempPath.length - 1) ].yLoc] = spriteObj.helicopterImg;
						vehicleScreen.repaint();
						
						mainMap.money -= 10000;
						bottomList.moneyLabel.setText( "$" + mainMap.money );
						bottomList.messageTextArea.append( String.valueOf( "Helicopter Sent to: [" + x + "]:[" + y + "]\n" ) );
						mainMap.tileList[x][y].buildingObj.helicopterPresent = true;
					}
					else
						bottomList.messageTextArea.append( "Please click a non-essential building!\n" );
					break;
				default:
					break;
				}
			}
		}
		
		
		switch( purchaseScreen )
		{
		case 0:// no purchase
			break;
		case 1:// building
			if( x == 20 && y == 17)
			{
				if( mainMap.money >= 10000 )
				{
					mainMap.startTile.buildingObj.reEnforced = true;
					bottomList.regBuildingPanel.setVisible( false );
					bottomList.emptyPanel.setVisible( true );
					mainMap.money -= 10000;
					bottomList.moneyLabel.setText( "$" + mainMap.money );
					bottomList.messageTextArea.append( String.valueOf( "Building Re-Enforced at: [" + mainMap.startTile.xLoc + "]:[" + mainMap.startTile.yLoc + "]\n" ) );
				}
				else
				{
					bottomList.messageTextArea.append( "You are out of money!\n" );
				}
			}
			break;
		case 2:// helipad
			if( mainMap.money >= 10000 )
			{
				if( x == 19 && y == 17 )
				{
					aboutToPurchase = 1;
					purchaseMade = 2;
				}
				else if ( x == 20 && y == 17 )
				{
					aboutToPurchase = 1;
					purchaseMade = 3;
				}
				
			}
			else
			{
				bottomList.messageTextArea.append( "You are out of money!\n" );
			}
			break;
		case 3:// fire station
			if( x == 20 && y == 17 )
			{
				if( mainMap.money >= 10000 )
				{
					aboutToPurchase = 1;
					purchaseMade = 1;
				}
				else
				{
					bottomList.messageTextArea.append( "You are out of money!\n" );
				}
			}
			break;
		case 4:// hospital purchase
			if( x == 20 && y == 17 )
			{
				if( mainMap.money >= 10000 )
				{
					aboutToPurchase = 1;
					purchaseMade = 0;
				}
				else
				{
					bottomList.messageTextArea.append( "You are out of money!\n" );
				}
			}
			break;
		case 5:// road purchase
			if( x == 19 && y == 17 )
			{
				if( mainMap.money >= 10000 )
				{
					mainMap.startTile.tent = true;
					mainMap.setTentValues( mainMap.startTile.xLoc, mainMap.startTile.yLoc );
					mainMap.startTile.img = spriteObj.tentImg;
					mapPanel.repaint( (mainMap.startTile.xLoc * 48 ), (mainMap.startTile.yLoc * 48 ), 48, 48 );
					bottomList.roadPanel.setVisible( false );
					bottomList.emptyPanel.setVisible( true );
					mainMap.money -= 10000;
					bottomList.moneyLabel.setText( "$" + mainMap.money );
					bottomList.messageTextArea.append( String.valueOf( "Tent Placed at: [" + mainMap.startTile.xLoc + "]:[" + mainMap.startTile.yLoc + "]\n" ) );
				}
				else
				{
					bottomList.messageTextArea.append( "You are out of money!\n" );
				}
			}
			else if ( x == 20 && y == 17 )
			{
				if( mainMap.money >= 10000 )
				{
					mainMap.startTile.roadBlock = true;
					mainMap.startTile.img = spriteObj.barricadeImg;
					mapPanel.repaint( (mainMap.startTile.xLoc * 48 ), (mainMap.startTile.yLoc * 48 ), 48, 48 );
					bottomList.roadPanel.setVisible( false );
					bottomList.emptyPanel.setVisible( true );
					mainMap.money -= 10000;
					bottomList.moneyLabel.setText( "$" + mainMap.money );
					bottomList.messageTextArea.append( String.valueOf( "Road Block Placed at: [" + mainMap.startTile.xLoc + "]:[" + mainMap.startTile.yLoc + "]\n" ) );
				}
				else
				{
					bottomList.messageTextArea.append( "You are out of money!\n" );
				}
			}
			break;
		default:
			break;
		}
			
	}

	private void purgePurchaseWindow()
	{
		bottomList.regBuildingPanel.setVisible( false );
		bottomList.roadPanel.setVisible( false );
		bottomList.emptyPanel.setVisible( false );
		bottomList.fireStationPanel.setVisible( false );
		bottomList.helipadPanel.setVisible( false );
		bottomList.hospitalPanel.setVisible( false );		
	}

	@Override
	public void mouseEntered(MouseEvent event) 
	{}

	@Override
	public void mouseExited(MouseEvent event)
	{}

	@Override
	public void mousePressed(MouseEvent event) 
	{}

	@Override
	public void mouseReleased(MouseEvent event)
	{}
	
	 // handle event when user moves mouse
    public void mouseMoved( MouseEvent event )
    {
  	  int xVal = event.getX() / 48;
  	  int yVal = event.getY() / 48;
  	  
  	  bottomList.cursorPositionLabel.setText( String.format( "Cursor position: [%d]:[%d]", 
          xVal, yVal ) );
  	  
  	  try
  	  {
	  	  if( mainMap.tileList[xVal][yVal].myName.equals( "B" ) )
	  	  {
	  		  bottomList.currentPopulationLabel.setText( String.format( "Current Population: %d", mainMap.tileList[xVal][yVal].buildingObj.curPop ) );
	  		  bottomList.currentDamageLabel.setText( String.format( "Current Damage: %d", mainMap.tileList[xVal][yVal].buildingObj.currentEndurance ) );
	  		  if( mainMap.tileList[xVal][yVal].buildingObj.diseaseRate > 0 )
	  			  bottomList.isDiseasedLabel.setText ( "Building diseased: true"  );
	  		  else
	  			  bottomList.isDiseasedLabel.setText ( "Building diseased: false"  );
	  		  
	  			  bottomList.isFortifiedLabel.setText( "Building fortified: " + String.valueOf( mainMap.tileList[xVal][yVal].buildingObj.reEnforced ) );
	  		  
	  		  if( mainMap.tileList[xVal][yVal].buildingObj.fireRate > 0 )
	  			  bottomList.isOnFireLabel.setText( "Building on fire: true" );
	  		  else
	  			bottomList.isOnFireLabel.setText( "Building on fire: false" );
	  	  }
	  	  else
	  	  {
	  		bottomList.currentPopulationLabel.setText( "Current Population: " );
	  		bottomList.currentDamageLabel.setText( "Current Damage: " );
	  		bottomList.isDiseasedLabel.setText ( "Building diseased: " );
	  		bottomList.isFortifiedLabel.setText( "Building fortified: " );
	  		bottomList.isOnFireLabel.setText( "Building on fire: " );
	  	  }
  	  }
  	  catch( ArrayIndexOutOfBoundsException e )
  	  {
  	  }
    } // end method mouseMoved

	@Override
	public void mouseDragged(MouseEvent arg0) 
	{}
	
	public void managePoppedDisasters()
	{
		DisasterNode temp = mainMap.randomDisasters.pullFromHeap();
		
		if( temp != null )
		{
			if( temp.disasterType.equals( "crumble" ) )
			{
				bottomList.messageTextArea.append( String.valueOf( "EVENT! Building at: [" + temp.startXLoc + "]:[" + temp.startYLoc + "] is beginning to crumble!\n" ) );
				mainMap.tileList[temp.startXLoc][temp.startYLoc].buildingObj.crumbleRate = temp.disasterRate;
			}
			else if( temp.disasterType.equals( "fire" ) )
			{
				bottomList.messageTextArea.append( String.valueOf( "EVENT! Building at: [" + temp.startXLoc + "]:[" + temp.startYLoc + "] has caught on fire!\n" ) );
				mainMap.tileList[temp.startXLoc][temp.startYLoc].buildingObj.fireRate = temp.disasterRate;
			}
		}
		
	}
	
	class CountdownTimerListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e) 
        {
            if (--mainMap.timeRemaining > 0) 
            {	
            	if( mainMap.timeRemaining < popDisasterTime )
            	{
            		popDisasterTime -= 15;
            		managePoppedDisasters();
            		
            		for( int i = 0; i < mainMap.buildingList.length; i++ )
            			mainMap.buildingList[i].buildingObj.runSimulatedDamage();
            	}
            	
            	int min = mainMap.timeRemaining / 60;
            	int sec = mainMap.timeRemaining % 60;
            	if( sec < 10 )
            		bottomList.timerLabel.setText(String.valueOf( min + ":0" + sec ) );
            	else
            		bottomList.timerLabel.setText(String.valueOf( min + ":" + sec ) );
            } else 
            {
            	bottomList.timerLabel.setText(String.valueOf( "0:00" ) );
            	// timer STOP
                countdownTimer.stop();
            }
        }
    }
}
